import pymysql


class DBUtil:
    def __init__(self,host,port,user,password):
        self.connet = pymysql.Connect(host=host,
                        port=port,
                        user=user,
                        password=password,
                        charset='utf8mb4',
                        cursorclass=pymysql.cursors.DictCursor
                        )

    # 查询方法
    def select(self,sql):
        cursor=self.connet.cursor()
        cursor.execute(sql)
        data=cursor.fetchall()
        self.connet.commit()
        cursor.close()
        return data

    # 关闭连接
    def close(self):
        if self.connet!=None:
            self.connet.close()

